package com.webproject.action.houseManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.houseManager.Building;
import com.webproject.entity.houseManager.Room;
import com.webproject.entity.houseManager.RoomInfo;
import com.webproject.entity.houseManager.RoomRepairInfo;
import com.webproject.service.houseManager.RoomManagerService;

public class RoomManagerAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Page mpage;
	private List<RoomInfo> roomInfos;
	private List<Room> rooms;
	public List<Room> getRooms() {
		return rooms;
	}
	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}
	public List<RoomInfo> getRoomInfos() {
		return roomInfos;
	}
	public void setRoomInfos(List<RoomInfo> roomInfos) {
		this.roomInfos = roomInfos;
	}
	public Page getMpage() {
		return mpage;
	}
	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}
	private RoomManagerService roomManagerService;
	public void setRoomManagerService(RoomManagerService roomManagerService) {
		this.roomManagerService = roomManagerService;
	}
	public String queryRoom(){
		mpage=getPage();
		roomManagerService.queryRoom(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String queryAllRooms(){
		mpage=getPage();
		mpage.setRow(10000);
		roomManagerService.queryRoom(mpage);
		rooms=(List<Room>) mpage.getRows();
		return SUCCESS;
	}
	public String editRoom(){
		Room room=getRoom();
		if(getOper().equals("add")){
			roomManagerService.saveRoom(room);
		}else if (getOper().equals("edit")){
			roomManagerService.updateRoom(room);
		}else if(getOper().equals("del")){
			roomManagerService.deleteRoom(room);
		}
		return SUCCESS;
	}
	private Room getRoom(){
		Room room=new Room();
		String id=getRequest().getParameter("id");
		String bId=getRequest().getParameter("bNumber");
		String riId=getRequest().getParameter("riArea");
		String roomCode=getRequest().getParameter("roomCode");
		String storey=getRequest().getParameter("storey");
		room.setbId(bId);
		room.setRiId(riId);
		if(isNotEmpty(id)){
			room.setId(Integer.parseInt(id));
		}
			room.setRoomCode(roomCode);
			if(isNotEmpty(storey)){
				room.setStorey(Integer.parseInt(storey));
			}
		return room;
	}
	
	public String queryRoomInfo(){
		mpage=getPage();
		roomManagerService.queryRoomInfo(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String queryRoomInfoByJson(){
		roomInfos=roomManagerService.queryRoomInfo();
		return SUCCESS;
	}
	public String editRoomInfo(){
		RoomInfo roomInfo=getRoomInfo(getOper());
		if(getOper().equals("add")){
			roomManagerService.saveRoomInfo(roomInfo);
		}else if (getOper().equals("edit")){
			roomManagerService.updateRoomInfo(roomInfo);
		}else if(getOper().equals("del")){
			roomManagerService.deleteRoomInfo(roomInfo);
		}
		return SUCCESS;
	}
	private RoomInfo getRoomInfo(String oper){
		RoomInfo roomInfo=new RoomInfo();
		String id=getRequest().getParameter("id");
		String area=getRequest().getParameter("area");
		String parlour=getRequest().getParameter("parlour");
		String room1=getRequest().getParameter("room");
		String kitchen=getRequest().getParameter("kitchen");
		String toilet=getRequest().getParameter("toilet");
		String icon=getRequest().getParameter("icon");
		roomInfo.setId(id);
		if(!oper.equals("del")){
			roomInfo.setArea(new Float(area));
			roomInfo.setKitchen(Integer.parseInt(kitchen));
			roomInfo.setParlour(Integer.parseInt(parlour));
			roomInfo.setRoom(Integer.parseInt(room1));
			roomInfo.setToilet(Integer.parseInt(toilet));
			roomInfo.setIcon(icon);
		}
		return roomInfo;
	}
	
	public String queryRoomRepairInfo(){
		mpage=getPage();
		String roomId=getRequest().getParameter("roomId");
		if(isNotEmpty(roomId)){
			Room room=new Room();
			room.setId(Integer.parseInt(roomId));
			mpage.setCondition(room);
			roomManagerService.queryRoomRepairInfo(mpage);
			setTotal(mpage);
		}
		return SUCCESS;
	}
	
	public String editRepairInfo(){
		RoomRepairInfo r=getRoomRepairInfo();
		if(getOper().equals("add")){
			roomManagerService.saveRoomRepairInfo(r);
		}else if (getOper().equals("edit")){
			roomManagerService.updateRoomRepairInfo(r);
		}else if(getOper().equals("del")){
			roomManagerService.deleteRoomRepairInfo(r);
		}
		return SUCCESS;
	}
	
	private RoomRepairInfo getRoomRepairInfo(){
		String id=getRequest().getParameter("id");
		String roomId=getRequest().getParameter("roomId");
		String registerTime=getRequest().getParameter("registerTime");
		String repairTime=getRequest().getParameter("repairTime");
		String finish=getRequest().getParameter("finish");
		String remark=getRequest().getParameter("remark");
		log.info("---------remark---------:"+remark);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		RoomRepairInfo r=new RoomRepairInfo();
		if(isNotEmpty(id)){
			r.setId(Integer.parseInt(id));
		}
		r.setFinish(Boolean.parseBoolean(finish));
		try {
			if(isNotEmpty(registerTime)){
				r.setRegisterTime(sdf.parse(registerTime));
			}
			if(isNotEmpty(repairTime)){
				r.setRepairTime(sdf.parse(repairTime));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(isNotEmpty(roomId)){
			r.setRoomId(Integer.parseInt(roomId));
		}
		r.setRemark(remark);
		return r;
	}
}
